//
//  H_UserManager.m
//  Honey
//
//  Created by 贾亚涵 on 16/5/7.
//  Copyright © 2016年 贾亚涵. All rights reserved.
//

#import "H_UserManager.h"


@implementation H_UserManager

@synthesize userModel;
//初始化
+(instancetype)Instance
{
    static H_UserManager * manager;
    //静态的执行一次的线程
    static dispatch_once_t once;
    
    dispatch_once(&once,^{
        manager = [self new];
    });
    return manager;
}

-(H_UserMemberModel *)userModel
{
    if (userModel == nil)
    {
        userModel = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getFilePathName]];
        
    }
    return userModel;
}
//保存登录状态信息
-(void)saveModel:(H_UserMemberModel *)model
{
    userModel = model;
    if (userModel != nil)
    {
        userModel.isLogin = YES;
    }
    [NSKeyedArchiver archiveRootObject:userModel toFile:[self getFilePathName]];
}

-(NSString *)getFilePathName
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentDirectory = [paths objectAtIndex:0];
    NSString * filePathName = [documentDirectory stringByAppendingPathComponent:@"H_user.zfh"];
    return filePathName;
}
//退出登录
-(void)logout
{
    if (userModel != nil)
    {
        userModel.isLogin = NO;
        
    }
    [NSKeyedArchiver archiveRootObject:userModel toFile:[self getFilePathName]];
}
//当前状态
-(BOOL)isLoginIn
{
    H_UserMemberModel * model = self.userModel;
    if (model && model.isLogin)
    {
        return YES;
    }
    return NO;
}
@end
